package com.example.moilapp.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;

import com.example.moilapp.Moil_Job_Detail;
import com.example.moilapp.R;
import com.example.moilapp.adapter.Moil_Pending_Task_Adapter;
import com.example.moilapp.signature.Moil_CaptureSignature;

public class Moil_Image_Load_Service  extends AsyncTask<String, Void, String> {

	public static final int SIGNATURE_ACTIVITY = 1;
	
	private ProgressDialog progdialod;
	private Activity activity;
	private Context context;
	
	Moil_Pending_Task_Adapter listAdapter;
	
    ExpandableListView expListView;
    List<String> listDataHeader = new ArrayList<String>();
    HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
	
	private ExpandableListView ExLstVw;
	
	private String JobNo;
	private String DoNo;
	// connection establish timeout (timeoutConnection)
	private int timeoutConnection = 5000;
	// socket timeout (timeoutSocket) 
	private int timeoutSocket = 45000;
	
	private static byte[] buff =new byte[1024];
	private String retval="";
	
	private int[] app_ids;
	
	String pass_job_id="";

	JSONArray jArray;
	private static final String SOAP_ACTION = "http://tempuri.org/GetSignatureImg";
	
    private static final String METHOD_NAME = "GetSignatureImg";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://t2a1.cloudapp.net/TrafficWorkflow/TraficWorkflowWebService.asmx";
	private static final String TAG = null;
	
	public Moil_Image_Load_Service(Activity activity,String JobNo,String DoNo,String jobid) {
		super();
		this.activity = activity;
		this.context = this.activity.getApplicationContext();
		this.JobNo = JobNo;
		this.DoNo = DoNo;
		this.pass_job_id = jobid;
		
		Log.e("GetSignatureImg JobNo", JobNo);
		Log.e("GetSignatureImg DoNo", DoNo);
		Log.e("GetSignatureImg pass_job_id", pass_job_id);
	}
	
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progdialod = ProgressDialog.show(this.activity, "Search", "Updating",true,false);
	}
	
	
	@Override
	protected String doInBackground(String... urls) {
		
		 try {

	            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

	            request.addProperty("JobID", pass_job_id);
	            request.addProperty("DoID", DoNo);

	            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	            envelope.dotNet=true;
	            envelope.setOutputSoapObject(request);

	            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
	            androidHttpTransport.call(SOAP_ACTION, envelope);
	            
	            SoapPrimitive resultSoapPrimitive;
	            resultSoapPrimitive = (SoapPrimitive) envelope.getResponse();
	                
	            String result = resultSoapPrimitive.toString();

	            retval = result;
	            
	            Log.d("return GetSignatureImg", retval);
	            
	            return retval;
	           
	        } catch (Exception e) {
	            
	       }
		 return retval;
		
	}
	
	@Override
	protected void onPostExecute(String result) {
		
		
		
		//Log.d("Post Exec", "POST EXEC");
		
		/*Toast toast = Toast.makeText(activity, result, Toast.LENGTH_SHORT);
		toast.show();*/
		
		//Log.d("Post tt", result);
		progdialod.dismiss();

		Log.d("Post Exec GetSignatureImg", result);
		
		//try {
			
			//JSONObject element01 = new JSONObject(result);
			//String respond = element01.getString("GetSignatureImgResult");

			//Log.d("respond", respond);
			
			ImageView myImage = (ImageView) activity.findViewById(R.id.signature_imageview);
			
			Button btnsignature = (Button)activity.findViewById(R.id.signature_dotline);
			
			
			if(!result.equals("null")){
				
				Log.d("Image Null/Not", "Image Not Null");
				
				myImage.setVisibility(View.VISIBLE);
				
				myImage.setImageBitmap(decodeBase64(result));
				
				//myImage.setImageBitmap(Bitmap.createScaledBitmap(decodeBase64(result), 1100, 650, false));
				
				btnsignature.setVisibility(View.GONE);
			}else{
				Log.d("Image Null/Not", "Image Null");
				myImage.setVisibility(View.GONE);
				btnsignature.setVisibility(View.VISIBLE);
			}

		//} catch (JSONException e) {
		//	e.printStackTrace();
		//}
		
	}
	
	public static Bitmap decodeBase64(String input) 
	{
	    byte[] decodedByte = Base64.decode(input, 0);
	    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
	}

}
